<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="timer"></div>
    <script>
        setInterval(function(){
            timer()
        },1000)

        var timer = function () {
            console.clear();//清除控制台的内容
            var time = new Date()
            var time11 = new Date("2022-11-11 00:00:00")
            var dTime = time11 - time;//双十一距离现在的毫秒数

            //一天的毫秒数24*60*60*1000，用总毫秒数除以一天的毫秒数得出天数
            var day = dTime / (24 * 60 * 60 * 1000);
            var day = Math.floor(day);//向下取整为天数
            if (day < 100) {
                day = "0" + day;
            } else if (day < 10) {
                day = "00" + day;
            };//判断天数是否足3位数或两两位数，不足的话在前面补零

            //计算减去天数后剩余的毫秒数，再算出剩余的小时数
            //一小时的毫秒数60*60*1000
            var dHour = dTime - day * (24 * 60 * 60 * 1000);
            var hour = dHour / (1000 * 60 * 60);
            var hour = Math.floor(hour);
            hour = hour < 10 ? "0" + hour : hour;//判断小时数是否是两位数，不足的话在前面补零

            //计算减去小时数后剩余的毫秒数，再算出剩余的分钟数
            //一分钟的毫秒数60*1000
            var dMinutes = dHour - hour * (60 * 60 * 1000);
            var minutes = dMinutes / (60 * 1000);
            var minutes = Math.floor(minutes);
            minutes = minutes < 10 ? "0" + minutes : minutes;

            //计算减去分钟数后剩余的毫秒数，再算出剩余的秒数
            //一秒=1000毫秒
            var dSecond = dMinutes - minutes * (60 * 1000);
            var second = dSecond / 1000;
            var second = Math.floor(second);
            second = second < 10 ? "0" + second : second;

            //组合
            var res = `距离2022双十一活动还有${day}天${hour}小时${minutes}分钟${second}秒`
            console.log(res);
        }
    </script>
</body>

</html>